package gui.workspace;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.border.EmptyBorder;
import resources.images.*;

//import Helper.ImageCanvas;

/**
 * Tab class for JTabbedPane in Workspace class. 
 * Allows for tabs to be closed using close button in this component
 * 
 * @author Troy Ferrell
 * @author David Colon-Smith
 */
public class WorkspaceTab extends JPanel implements ActionListener
{
	private static final long serialVersionUID = 1L;
	
	private static final int CLOSE_ICON_SIZE = 20;
	private static final String CLOSE_ICON_PATH = "src/resources/images/icon_button_close.gif";
	private final Workspace myWorkspace;
	
	public WorkspaceTab(final Workspace frame, String title)
	{
		myWorkspace = frame;
		
		this.setOpaque(false);
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		addCloseButton(title);
		
		setVisible(true);
	}

	private void addCloseButton(String name) {
		JButton closeButton = new JButton(new ImageIcon(CLOSE_ICON_PATH));
		closeButton.setPreferredSize(new Dimension(CLOSE_ICON_SIZE, CLOSE_ICON_SIZE));
		closeButton.addActionListener(this);
	
		add(new JLabel(name));
		add(closeButton);
	}
	
	public void actionPerformed(ActionEvent e)
	{	
		int index = myWorkspace.indexOfTabComponent(this);
		if(index != -1)
			myWorkspace.removeTab(index);
	}
	
}
